<?php

namespace app\index\controller;

use think\Db;
use think\facade\Config;

class DataBaseDemo{
	function __construct(){
		Config::set('app_debug',true);
		Config::set('app_trace',true);
	}
	
	/*
	 * 单条查询
	 */
	function find(){
		$res = Db::table('student')
		->field(['id'=>'student_id'])		//不支持中文别名，中文别名用'id as 编号'
		->where('id','=','4')				//查询主键时可以写find(4)
		->find();
		dump($res);
	}
	
	/**
	 * 多条查询
	 */
	function select(){
		$res = Db::table('student')
// 		->where(['course'=>'说','grade'=>'88'])
		->where(
				[
					['course','=','说'],
					['grade','>=','60']
				]
				)
		->select();
		dump($res);
	}
	
	/**
	 * 单条插入
	 */
	function insert(){
		$data = ['name'=>'张鹤伦','email'=>'email','course'=>'唱','grade'=>'100'];
		$res = Db::table('student')
		->insert($data,true);
		
		//可以对参数进行过滤
		/* $res = Db::table('student')
		->data($data)
		->insert(); */
		
		return $res;
	}
	
	/**
	 * 多条插入
	 */
	function inserts(){
		$data = [
				['name'=>'郎鹤焱'],
				['name'=>'孟鹤堂'],
				['name'=>'李鹤彪'],
				['name'=>'姬鹤武'],
				['name'=>'孙鹤宝'],
		];
		return Db::table('student')->insertAll($data);
	}
	
	/**
	 * 更新
	 */
	function update(){
		$res = Db::table('student')
		->where(['name'=>'郎鹤焱'])
		->update(['name'=>'郎鹤焱','email'=>'email15']);		//主键条件可以直接写在data里
		dump($res);
	}
	
	/**
	 * 原生查询
	 */
	function query(){
		$sql = 'select * from student where id in (1,2,3)';
		$res = Db::query($sql);
		dump($res);
	}
	
	/**
	 * 原生增删改
	 */
	function execute(){
		$sql = "update student set email='email' where email=''";
		$res = Db::execute($sql);
		return $res;
	}
}